package com.lry.basic.algorithm.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AbsSort {

    public static void main(String[] args) {
        System.out.println(new AbsSort().absSort(100, Arrays.asList(95, 96, 97, 98, 99, 101, 102, 103, 104, 105)));
    }



    private List<Integer> absSort(int h,List<Integer> list){
        Helper[] helpers = new Helper[200];
        for (Integer hi : list) {
            int index = Math.abs(h-hi);
            Helper helper = helpers[index];
            if(null==helper){
                helper = new Helper(hi);
            }else{
                if(hi>helper.small){
                    helper.big = hi;
                }else{
                    helper.big = helper.small;
                    helper.small = hi;
                }
            }
            helpers[index] = helper;
        }

        List<Integer> list1 = new ArrayList<>();
        for (Helper helper : helpers) {
            if(null==helper){
                continue;
            }
            list1.add(helper.small);
            if(helper.big!=-1){
                list1.add(helper.big);
            }
        }
        return list1;
    }

    class Helper{
        public int small = -1;
        public int big = -1;

        public Helper(int small){
            this.small = small;
        }
    }
}

